import { isUndefined, isNull, PagedList, PageParams } from "mmb";
import * as brand_bank_cardpb from "../grpc-client/service/brand_bank_card_pb";


import {
  getBgsBrandBankCardOpSafely,
  getMetadata,
  getOptionsOfLight,
  wrapperGrpcError,
} from "../grpc-client/grpcAdapter";
import GrpcErrCode from "../grpc-client/grpcErrCode";
import {
  BrandBankCard,
  UpsertBrandBankCardParams,
} from "./declareTypes"

/****************************************************************************************/
/*                                   brand_bank_card entity                             */
/****************************************************************************************/

const BrandBankCardPbToObject = (
  pb: brand_bank_cardpb.BrandBankCardEntity
): BrandBankCard => {
  return {
    brandBankCardId: `${pb.getId()}`,
    brandId: `${pb.getBrandId()}`,
    userId: `${pb.getUserId()}`,
    accountType: `${pb.getAccountType()}`,
    accountBank: pb.getAccountBank(),
    bankAddressCode: pb.getBankAddressCode(),
    bankName: pb.getBankName(),
    accountNumber: pb.getAccountNumber(),
    verifyResult: pb.getVerifyResult(),
  };
};

/****************************************************************************************/
/*                                   brand_bank_card cmd                                */
/****************************************************************************************/

export const upsertBrandBankCard = async ({
  brandId,
  userId,
  accountType,
  accountBank,
  bankAddressCode,
  bankName,
  accountNumber,
}: UpsertBrandBankCardParams): Promise<BrandBankCard> => {
  const cl = getBgsBrandBankCardOpSafely();
  const req = new brand_bank_cardpb.UpsertBrandBankCardRequest();
  req.setBrandId(Number(brandId));
  req.setUserId(Number(userId));
  req.setAccountType(accountType);
  req.setAccountBank(accountBank);
  req.setBankAddressCode(bankAddressCode);
  req.setBankName(bankName);
  req.setAccountNumber(accountNumber);
  

  return new Promise((resolve, reject) => {
    cl.upsertBrandBankCard(
      req,
      getMetadata(),
      getOptionsOfLight(),
      (err, res) => {
        if (!isNull(err)) {
          reject(wrapperGrpcError(err.details));
          return;
        }
        const opRet = res.getOpRet()!;
        if (!opRet.getSuccess()) {
          reject(wrapperGrpcError(opRet.getMsg()));
          return;
        }
        resolve(BrandBankCardPbToObject(res.getBrandBankCard()!));
      }
    );
  });
};


/****************************************************************************************/
/*                                   brand_bank_card query                              */
/****************************************************************************************/

export const getBrandBankCard = async (
  brandBankCardId: string
): Promise<BrandBankCard> => {
  const cl = getBgsBrandBankCardOpSafely();
  const req = new brand_bank_cardpb.GetBrandBankCardRequest();

  req.setId(Number(brandBankCardId));

  return new Promise((resolve, reject) => {
    cl.getBrandBankCard(req, getMetadata(), getOptionsOfLight(), (err, res) => {
      if (!isNull(err)) {
        reject(wrapperGrpcError(err.details));
        return;
      }
      const opRet = res.getOpRet()!;
      if (!opRet.getSuccess()) {
        reject(wrapperGrpcError(opRet.getMsg()));
        return;
      }
      resolve(BrandBankCardPbToObject(res.getBrandBankCard()!));
    });
  });
};

export const getLatestBrandBankCardByBrandID = async (
  brandId: string
): Promise<BrandBankCard | undefined> => {
  const cl = getBgsBrandBankCardOpSafely();
  const req = new brand_bank_cardpb.GetLatestBrandBankCardByBrandIDRequest();

  req.setBrandId(Number(brandId));

  return new Promise((resolve, reject) => {
    cl.getLatestBrandBankCardByBrandID(req, getMetadata(), getOptionsOfLight(), (err, res) => {
      if (!isNull(err)) {
        reject(wrapperGrpcError(err.details));
        return;
      }
      const opRet = res.getOpRet()!;
      if (!opRet.getSuccess()) {
        if (opRet.getCode() === GrpcErrCode.NOT_FOUND) {
          resolve(undefined);
          return;
        } else {
          reject(wrapperGrpcError(opRet.getMsg()));
          return;
        }
      }
      resolve(BrandBankCardPbToObject(res.getBrandBankCard()!));
    });
  });
};

